function Promise(callback) {
    let that = this;
    // 默认状态
    this.PromiseState = 'pending';
    // 默认结果值
    this.PromiseResult = undefined

    this.arrs = [];

    function resolve(velue) {
        if (that.PromiseState !== 'pending') return
        // 成功状态
        that.PromiseState = 'fulfilled';
        // 成功结果值
        that.PromiseResult = velue;

        if(that.arrs.length > 0){
            setTimeout(()=>{
                that.arrs.forEach(function (item) {
                    item.onResolved(velue)
                })
            })
        }
    }

    function reject(reason) {
        if (that.PromiseState !== 'pending') return
        // 失败状态
        that.PromiseState = 'rejected';
        // 失败结果值
        that.PromiseResult = reason;

        if(that.arrs.length > 0){
            setTimeout(()=>{
                that.arrs.forEach(function (item) {
                    item.onReject(reason)
                })
            })
        }
    }
    callback(resolve, reject);
}
Promise.prototype.then = function (onResolved, onReject) {

    if (typeof onReject !== 'function') {
        onReject = reason => {
            throw reason;
        }
    }
    if (typeof onResolved !== 'function') {
        onResolved = velue => {
            return velue
        }
    }

    return new Promise((resolve, reject) => {

        if (this.PromiseState === 'fulfilled') {
            setTimeout(()=>{
                try{let res = onResolved(this.PromiseResult)
                    if (res instanceof Promise) {
                        res.then(v => {
                            resolve(v)
                        }, r => {
                            reject(r)
                        })
                    } else {
                        resolve(res)
                    }}catch(e){
                        reject(e)
                    }
            })
        }
        if (this.PromiseState === 'rejected') {
           setTimeout(()=>{
            try{let res = onReject(this.PromiseResult)
                if (res instanceof Promise) {
                    res.then(v => {
                        resolve(v)
                    }, r => {
                        reject(r)
                    })
                } else {
                    reject(res)
                }}catch(e){
                    reject(e)
                }
           })
        }
        if (this.PromiseState === 'pending') {
            this.arrs.push({
                onResolved: function (velue) {
                    try{let res = onResolved(velue)
                    if (res instanceof Promise) {
                        res.then(v => {
                            resolve(v)
                        }, r => {
                            reject(r)
                        })
                    } else {
                        resolve(res)
                    }}catch(e){
                        reject(e)
                    }
                },
                onReject: function (reason) {
                    try{let res = onReject(reason)
                    if (res instanceof Promise) {
                        res.then(v => {
                            resolve(v)
                        }, r => {
                            reject(r)
                        })
                    } else {
                        reject(res)
                    }}catch(e){
                        reject(e)
                    }
                }
            })
        }
    })
}
Promise.prototype.catch = function(onReject){
    return this.then(undefined,onReject)
}